package net.com.sunkey.core;

import java.sql.Connection;
import java.sql.SQLException;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

/**
 * @author kohachiro
 *
 */
public class DBManager {
	/**
	 * 
	 */
	public static Connection connection = null;
	/**
	 * 
	 */
	public DBManager() {

	}
	public void init() {
		if (AbstractServer.jdbcUrl=="")
			return;
		try {
			Class.forName(AbstractServer.jdbcDriver);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}	
		try {
			// setup the connection pool
			BoneCPConfig config = new BoneCPConfig();
			config.setJdbcUrl(AbstractServer.jdbcUrl); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
			config.setMinConnectionsPerPartition(1);
			config.setMaxConnectionsPerPartition(5);
			config.setPartitionCount(1);
			BoneCP connectionPool = new BoneCP(config); // setup the connection pool
			
			connection = connectionPool.getConnection(); // fetch a connection
			
			if (connection != null){		
				System.out.println("DB Connection:"+AbstractServer.jdbcDriver);
//				statement = connection.createStatement();
//				ResultSet rs = stmt.executeQuery("select CURRENT_DATE"); // do something with the connection.
//				while(rs.next()){
//					System.out.println(rs.getString(1)); // should print out "1"'
//				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		
	}

}
